/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.activity;

import java.util.Set;

import com.cleangwt.bootstrap.client.ui.Modal;
import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.page.client.PageContext;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.resource.i18n.ShoppingManagerMessages;
import com.cleangwt.user.client.activity.ForwardableActivity;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.TextBox;
import com.cleangwt.user.client.ui.ValueCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class CwShoppingPayup extends ForwardableActivity implements ProvidesSource {

  private ShoppingManagerMessages c = GWT.create(ShoppingManagerMessages.class);
  private HTML lblProducts = new HTML();

  public CwShoppingPayup() {
    Button btnBack = new Button(c.back());
    Button btnPayup = new Button(c.payup());

    btnBack.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        forward(0);
      }
    });

    btnPayup.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        // final TextBox txtbAddress = new TextBox();
        Modal.input(c.paymentAddress(), new TextBox(), new ValueCallback<String>() {

          @Override
          public void onValueCallback(String option) {
            Modal.message(c.thanks() + option, new ValueCallback<String>() {

              @Override
              public void onValueCallback(String option) {
                PageContext.refresh();
              }
            });
          }

        });
      }
    });
    initWidget(Lay.flow(lblProducts, Lay.pullRight(Lay.p(btnBack, btnPayup))));
  }

  @Override
  public void onActive(int relatedIndex) {
    CwShoppingCart cart = getActivity(0);
    this.setProducts(cart.getSelectedProcudts());
  }

  public void setProducts(Set<SimpleObject> value) {
    StringBuilder sb = new StringBuilder(c.chooseProducts(value.size()));
    int total = 0;
    for (SimpleObject p : value) {
      sb.append(p.get("name") + "<br/>");
      total += (Integer) p.get("price");
    }
    sb.append(c.totalAmount() + total);
    lblProducts.setHTML(sb.toString());
  }

}
